﻿
#pragma once

namespace 客户端 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Form1 摘要
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: 在此处添加构造函数代码
			//
		}

	protected:
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	protected: 
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::ComponentModel::BackgroundWorker^  backgroundWorker1;

	private:
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要
		/// 使用代码编辑器修改此方法的内容。
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
			this->SuspendLayout();
			// 
			// textBox1
			// 
			this->textBox1->AcceptsReturn = true;
			this->textBox1->AcceptsTab = true;
			this->textBox1->Location = System::Drawing::Point(24, 303);
			this->textBox1->MaxLength = 255;
			this->textBox1->Multiline = true;
			this->textBox1->Name = L"textBox1";
			this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
			this->textBox1->Size = System::Drawing::Size(482, 93);
			this->textBox1->TabIndex = 1;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(525, 373);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(62, 23);
			this->button1->TabIndex = 2;
			this->button1->Text = L"发送";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(789, 373);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(75, 23);
			this->button2->TabIndex = 3;
			this->button2->Text = L"退出";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(636, 373);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(75, 23);
			this->button3->TabIndex = 4;
			this->button3->Text = L"切换用户";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->BackColor = System::Drawing::Color::Transparent;
			this->label2->Font = (gcnew System::Drawing::Font(L"隶书", 15, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label2->ForeColor = System::Drawing::SystemColors::ButtonHighlight;
			this->label2->Location = System::Drawing::Point(543, 63);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(109, 20);
			this->label2->TabIndex = 5;
			this->label2->Text = L"用户名称：";
			// 
			// label3
			// 
			this->label3->BackColor = System::Drawing::Color::Transparent;
			this->label3->Font = (gcnew System::Drawing::Font(L"隶书", 15, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label3->ForeColor = System::Drawing::SystemColors::ButtonHighlight;
			this->label3->Location = System::Drawing::Point(658, 63);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(220, 23);
			this->label3->TabIndex = 6;
			// 
			// textBox2
			// 
			this->textBox2->BackColor = System::Drawing::SystemColors::Window;
			this->textBox2->Location = System::Drawing::Point(24, 34);
			this->textBox2->Multiline = true;
			this->textBox2->Name = L"textBox2";
			this->textBox2->ReadOnly = true;
			this->textBox2->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
			this->textBox2->Size = System::Drawing::Size(484, 253);
			this->textBox2->TabIndex = 7;
			// 
			// backgroundWorker1
			// 
			this->backgroundWorker1->WorkerReportsProgress = true;
			this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::backgroundWorker1_DoWork);
			this->backgroundWorker1->ProgressChanged += gcnew System::ComponentModel::ProgressChangedEventHandler(this, &Form1::backgroundWorker1_ProgressChanged);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"$this.BackgroundImage")));
			this->ClientSize = System::Drawing::Size(890, 423);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->textBox1);
			this->Name = L"Form1";
			this->Text = L"欢迎";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
char * Convert(System::String ^ str, int len)
{
	 pin_ptr<const wchar_t> wch = PtrToStringChars(str);
	 size_t convertedChars = 0;
	 size_t sizeInBytes = ((len +1) * 2);
	 char * ch = (char *)malloc(sizeInBytes);
	 errno_t err = 0;
	 err = wcstombs_s(&convertedChars,ch, sizeInBytes,wch, sizeInBytes);
	 if (err != 0)
		 MessageBox::Show("Input read failed!\n");
	 return ch;
}
char * received;
	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
				flag=2;
				 Close();
			 }
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
			  flag=0;
			  Close();

		 }
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
			 flag=2;
			 this->label3->Text = System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)username);
			 received=(char *)malloc(sizeof(char)*DEFAULT_BUFLEN);
			 memset(received,0,sizeof(char)*DEFAULT_BUFLEN);
			 // Start the asynchronous operation.
			 backgroundWorker1->RunWorkerAsync();
		 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 char *sendbuf=NULL,buf[DEFAULT_BUFLEN]="3\t";
			 buf[0]='3';buf[1]='\t';for(int i=2;i<DEFAULT_BUFLEN;i++)buf[i]='\0';
			 for(int i=0;i<DEFAULT_BUFLEN;i++)recvbuf[i]='\0';
			 if(!(textBox1->TextLength))return;
			 if((sendbuf=(char*)malloc(sizeof(char)*DEFAULT_BUFLEN))==NULL){MessageBox::Show("malloc failed! Please restart program!");exit(1);}
			 strcpy(sendbuf,strcat(buf,Convert(textBox1->Text,textBox1->TextLength)));
			 textBox1->Clear();
			 // Send an initial buffer
				 iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
				 if (iResult == SOCKET_ERROR) {
					 MessageBox::Show("Send failed with error!");
					 closesocket(ConnectSocket);
					 WSACleanup();
					 return;
				 }
		 }

private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
			 BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);
			 // Receive until the peer closes the connection
			 do {
				 for(int i=0;i<DEFAULT_BUFLEN;i++)recvbuf[i]='\0';
				 iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
				 if ( iResult > 0 )
				 {
					 if(recvbuf[0]=='\0')iResult=recv(ConnectSocket,recvbuf,recvbuflen,0);
					 strcpy(received,recvbuf);
					 worker->ReportProgress(1);
				 }
				 else if ( iResult == 0 )
				 {
					 MessageBox::Show("Connection closed!");
					 closesocket(ConnectSocket);
					 WSACleanup();
					 free(received);
					 Close();
				 }
				 else
				 {
					 MessageBox::Show("Recv failed with error!");
					 closesocket(ConnectSocket);
					 WSACleanup();
					 free(received);
					 Close();
				 }
			 } while( iResult > 0 );
		 }
private: System::Void backgroundWorker1_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
			 // This event handler updates the progress bar.
			 this->textBox2->AppendText(System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)strtok(received,"\n"))+"\n");
			 this->textBox2->AppendText(System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)strtok(NULL,"\a"))+"\n");
			 memset(received,0,sizeof(char)*DEFAULT_BUFLEN);
		 }
};
}